package com.shure.modules.docs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shure.common.query.condition.Condition;
import com.shure.modules.docs.entity.Knowledge;
import com.shure.modules.docs.query.FilesQuery;
import com.shure.modules.docs.query.KnowledgeQuery;
import com.shure.modules.docs.service.KnowledgeService;

import io.swagger.v3.oas.annotations.parameters.RequestBody;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/knowledge")
public class KnowledgeController {

    private final KnowledgeService knowledgeService;

    public KnowledgeController(KnowledgeService knowledgeService) {
        this.knowledgeService = knowledgeService;
    }

    @GetMapping("/page")
    public ResponseEntity<Page<Knowledge>> page(KnowledgeQuery query) {
        LambdaQueryWrapper<Knowledge> wrapper = Condition.getLambdaQueryWrapper1(new FilesQuery());
        wrapper.orderByDesc(Knowledge::getUpdateAt);
        return ResponseEntity.ok(knowledgeService.page(query.getPage(), wrapper));
    }

    @GetMapping("{id}")
    public ResponseEntity<Knowledge> getById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(knowledgeService.getById(id));
    }

    @PostMapping
    public ResponseEntity<Knowledge> save(@RequestBody Knowledge knowledge) {
        return ResponseEntity.ok(knowledgeService.save(knowledge) ? knowledge : null);
    }

    @PutMapping("{id}")
    public ResponseEntity<Knowledge> update(@PathVariable("id") Long id, @RequestBody Knowledge knowledge) {
        return ResponseEntity.ok(knowledgeService.updateById(knowledge) ? knowledge : null);
    }

    @DeleteMapping("{id}")
    public ResponseEntity<Boolean> delete(@PathVariable("id") Long id) {
        return ResponseEntity.ok(knowledgeService.removeById(id));
    }


}
